package yxy.game.pm2.command.passbarrier;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Format;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PassBarrierMessage;
import yxy.game.pm2.module.passbarrier.PassbarrierHandle;
import yxy.game.pm2.module.passbarrier.PassbarrierPlayerModule;

import java.util.ArrayList;
import java.util.Date;


/****************************************
 * @name CMD_PassBarrierChallenge
 * @description 斩关夺隘主挑战
 * @author 梁健彬
 * @since 2022-09-05
 ***************************************/
@MessageType(proto = Command.PassBarrierChallenge, classtype = PassBarrierMessage.PassBarrierChallengeRequest.class)
public class CMD_PassBarrierChallenge extends Command<PassBarrierMessage.PassBarrierChallengeRequest> {

    @Override
    public void execute(MessageSource messageSource, PassBarrierMessage.PassBarrierChallengeRequest request) throws Exception {
        Date date = new Date();

        String playerCode = messageSource.getTag();
        PassBarrierMessage.PassBarrierChallengeResponse.Builder response = PassBarrierMessage.PassBarrierChallengeResponse.newBuilder();
        try {
            PassbarrierPlayerModule module = PassbarrierPlayerModule.get(playerCode);
            if (module == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            //挑战次数
            int lessChallengeNum = module.getLessChallengeNum();
            int challengePurchaseNum = module.getChallengePurchaseNum();
            ArrayList<Format> formats = module.getFormats();
            if (lessChallengeNum + challengePurchaseNum <= 0) {
                send(messageSource, response, Result.PassBarrierCantChallenge);
                return;
            }

            if (formats.size() < 3) {
                send(messageSource, response, Result.PassBarrierNeedSaveFormat);
                return;
            }

            PassbarrierHandle handle = new PassbarrierHandle();

            //todo 1.返回录像信息和是否胜利
            handle.battleResponseExecute(playerCode, module, response);

            //todo 2.返回主页面信息
            PassBarrierMessage.PassBarrierInfoMsg.Builder passBarrierInfo = handle.getPassBarrierInfo(module);
            response.setMsgData(passBarrierInfo);

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }

        Date date1 = new Date();
        System.out.println("CMD_PassBarrierChallenge:"+(date1.getTime()-date.getTime()));

    }

    private void send(MessageSource messageSource, PassBarrierMessage.PassBarrierChallengeResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


}
